//
//  BindingPhoneTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/16.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "BindingPhoneTableViewController.h"
#import "BindingPhoneTableViewCell.h"
#import "bindingFooterView.h"
#import "JudgePhoneNumberBelong.h"
#import "GetPhoneNumber.h"

@interface BindingPhoneTableViewController () <UITextFieldDelegate,didClickYanZhengMaButtonDelegate,didClickConfirmButtonDelegate>
{
    NSString *_phoneNumber;
}

@property (nonatomic,strong)NSArray *PlaceholderArray;

@property (nonatomic,strong)UITextField *textField;

@property (nonatomic,strong)NSMutableArray *textFieldArray;

@end

static NSString *const reuseIdentifier = @"reuseIdentifier";

@implementation BindingPhoneTableViewController

- (NSMutableArray *)textFieldArray {
    if (!_textFieldArray) {
        _textFieldArray = [NSMutableArray array];
    }
    return _textFieldArray;
}

- (NSArray *)PlaceholderArray {
    if (!_PlaceholderArray) {
        NSString *accountPath = [[NSBundle mainBundle] pathForResource:@"BindingPhoneText" ofType:@"plist"];
        
        _PlaceholderArray = [NSArray arrayWithContentsOfFile:accountPath];
    }
    return _PlaceholderArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"绑定手机号码";
    self.edgesForExtendedLayout = UIRectEdgeNone;
//    self.tableView.scrollEnabled = NO;
    
//    self.tableView.bounces = NO;
    
    self.tableView.backgroundColor = BACKGROUND_COLOR;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 2;
    } else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BindingPhoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    if (cell == nil) {
        cell = [[BindingPhoneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSString *imageString = [NSString stringWithFormat:@"shoujibangding_%ld",indexPath.row + indexPath.section + 1];
    cell.leftImageView.image = [UIImage imageNamed:imageString];
    
    cell.textField.tag = indexPath.row + indexPath.section;
    [cell.textField addTarget:self action:@selector(didClickTextFieldChange:) forControlEvents:UIControlEventEditingChanged];
    cell.textField.delegate = self;
    cell.textField.placeholder = self.PlaceholderArray[indexPath.row + indexPath.section];
    [self.textFieldArray addObject:cell.textField];
    
    if(indexPath.section == 0 && indexPath.row == 0) {
        cell.rightLabel.hidden = NO;
        cell.textField.hidden = YES;
        cell.rightLabel.text = USERNAME;
    }
    
    if (indexPath.section == 1 && indexPath.row == 1) {
        cell.delegate = self;
        cell.rightButton.hidden = NO;
    }
    
    if (indexPath.section == 2) {
        
        cell.textField.tag = 3;
        cell.textField.placeholder = [self.PlaceholderArray lastObject];
        cell.leftImageView.image = [UIImage imageNamed:@"shoujibangding_4"];
        cell.textField.delegate = self;
        cell.textField.keyboardType = UIKeyboardTypeDefault;
        cell.textField.secureTextEntry = YES;
    }
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        bindingFooterView *footer = [[bindingFooterView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
        footer.delegaete = self;
        return footer;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    if (section == 2) {
        return HEIGHT/5;
    }
    
    return 0;
}

- (void)didClickTextFieldChange:(UITextField *)textField {
    NSLog(@"%ld",textField.tag);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.0001;
    } else {
        return 3;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self clearTextField];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [self clearTextField];
//    NSLog(@"%ld",self.textField.tag);
}

- (void)clearTextField {
    for (UITextField *textField in self.textFieldArray) {
        [textField resignFirstResponder];
    }
}

- (void)didClickViewClearTextField {
    [self clearTextField];
}

/**
 *  获取验证码
 */
- (void)didClickYanZhengMaButton:(UIButton *)button {
    
    UITextField *bindPhoneNum = self.textFieldArray[1];
    
    _phoneNumber = bindPhoneNum.text;
    
//    [[GetPhoneNumber SharedGetPhoneNumber] getPhoneVerifyNumber:button andPhoneNumber:_phoneNumber];
    [[GetPhoneNumber SharedGetPhoneNumber] getPhoneVerifyNumber:button andPhoneNumber:_phoneNumber andView:self.view];
}

/**
 *  确定按钮
 */
- (void)didClickConfirmButton:(UIButton *)confirmButton {
    [self clearTextField];
    NSLog(@"确认");
    
    UITextField *passWordTextField = self.textFieldArray.lastObject;
    
    UITextField *verifyTextField = self.textFieldArray[2];
    
    UITextField *phoneNumber = self.textFieldArray[1];
    
    if (passWordTextField.text.length == 0 || verifyTextField.text == 0 || phoneNumber.text.length == 0) {
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确保信息输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        [self.view addSubview:hud];
    } else {
        NSDictionary *params = @{
                                 @"username":USERNAME,
                                 @"password":passWordTextField.text,
                                 @"verify":verifyTextField.text
                                 };
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"AppUser/Setphone" completeBlock:^(id object) {
            
//            if (JudgeBackDataCode) {
//                
//                MBProgressHUD *hud = [MBProgressHUD showMessage:@"绑定手机号成功" toView:self.view dimBackground:NO AndCompleteBlock:^(id object) {
//                    [self.navigationController popViewControllerAnimated:YES];
//                }];
//                
//                [self.view addSubview:hud];
//                
//            } else {
//                NSString *message = [object objectForKey:@"message"];
//                
//                MBProgressHUD *hud = [MBProgressHUD showMessage:message toView:self.view dimBackground:NO AndCompleteBlock:nil];
//                
//                [self.view addSubview:hud];
//            }

            [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"绑定手机号成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
                
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }];

    }
    
}

/**
 *  验证码接口
 */

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.textFieldArray.lastObject) {
        [self didClickConfirmButton:nil];
    }
    return YES;
}
@end
